package org.kolonitsky.css;

import org.apache.batik.css.parser.Parser;
import org.kolonitsky.css.cssdoc.CssDocXmlHandler;
import org.w3c.css.sac.*;

import java.io.*;

/**
 * Hello world!
 */
public class CssToXml {

    public static void main(String[] args) {
        try {
            //generateXml(new FileReader("D:\\prj\\java-css\\java-css-doc\\src\\main\\resources\\template\\default\\styles\\css_doc.css"), "D:/test.01");
            generateXml(
                    new FileReader("D:\\projects\\hp\\java-css-doc\\src\\test\\resources\\ui-core_02.css"),
                    "D:\\projects\\hp\\java-css-doc\\src\\test\\resources\\org\\kolonitsky\\css\\cssdoc\\CssDocHtmlReport\\ui-core_02");
        } catch (FileNotFoundException e) {
            e.printStackTrace();  // TODO
        }
    }

    public static String generateXml(Reader reader) {
        StringWriter writer = new StringWriter();

        generateXml(reader, writer);

        return writer.toString();
    }

    public static void generateXml(Reader reader, Writer writer) {
        Parser parser = new MyCssParser();
        CssToXmlDocHandler handler = new CssDocXmlHandler(writer);
        parser.setDocumentHandler(handler);

        try {
            parser.parseStyleSheet(new InputSource(reader));
        } catch (IOException e) {
            e.printStackTrace();
        }
        handler.flush();
    }

    public static void generateXml(Reader reader, String fileName) {
        try {
            generateXml(reader, new FileWriter(fileName + ".xml"));
        } catch (IOException e) {
            e.printStackTrace();  // TODO
        }
    }
}
